import axios from "axios";

const axiosInstance = axios.create({
    baseURL: "http://localhost:8085",
});

axiosInstance.interceptors.request.use((config) => {
    let token =  window.sessionStorage.getItem('token')
    if (token) {
        config.headers.Authorization = 'Bearer ' + window.sessionStorage.getItem('token')
    }
    return config;
});

axiosInstance.interceptors.response.use(
    response => {
        let data = response.data;
        if (data.code === 401) {
            sessionStorage.clear()
        } else {
            return data;
        }
    },
    error => {
        console.log(error)
    }
);

const dataHandle = response => {
    return response;
};

const innerGet = function() {
    return axiosInstance.get.apply(this, arguments).then(dataHandle);
};

const innerPost = function() {
    return axiosInstance.post.apply(this, arguments).then(dataHandle);
};

const innerDelete = function() {
    return axiosInstance.delete.apply(this, arguments).then(dataHandle);
};

const innerPut = function() {
    return axiosInstance.put.apply(this, arguments).then(dataHandle);
};

export const pureGet = function(url) {
    return innerGet(url);
};

export const pureDelete = function(url) {
    return innerDelete(url);
};

export const get = function(url, params) {
    return innerGet(url, {
        params: params
    });
};

export const post = function(url, data) {
    return innerPost(url, data, {
        headers: {
            "Content-Type": "application/json"
        }
    });
};

export const postForm = function (url, data) {
    return innerPost(url, data, {
        headers: {
            "Content-Type": "multipart/form-data"
        }
    });

}

export const del = function(url, params) {
    return innerDelete(url, {
        params: params
    });
};

export const put = function(url, data) {
    return innerPut(url, data, {
        headers: {
            "Content-Type": "application/json"
        }
    });
};
